<script setup>
import { ref, onMounted } from 'vue'
import HomePanel from './HomePanel.vue'
import { listProduct } from '@/apis/product.js'

// 商品列表数据
const newGoodsList = ref([])

// 获取商品列表
const fetchProductList = async () => {
  try {
    const res = await listProduct()
    // 只取前4个商品
    newGoodsList.value = (res.data || []).slice(0, 4).map(item => ({
      id: item.pid,
      name: item.pname,
      picture: item.pimage1,
      // 注意：后端数据没有价格字段，这里暂时设置为固定值
      price: item.price
    }))
  } catch (error) {
    console.error('获取商品列表失败:', error)
  }
}

onMounted(() => {
  fetchProductList()
})
</script>

<template>
  <HomePanel title="商城上新" subTitle="新鲜出炉 品质靠谱">
    <template #main>
      <ul class="goods-list">
        <li v-for="item in newGoodsList" :key="item.id">
          <RouterLink :to="`/detail/${item.id}`">
            <img :src="item.picture" :alt="item.name" />
            <p class="name">{{ item.name }}</p>
            <p class="price">&yen;{{ item.price }}</p>
          </RouterLink>
        </li>
      </ul>
    </template>
  </HomePanel>
</template>

<style scoped lang='scss'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;

  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    transition: all 0.5s;

    &:hover {
      transform: translate3d(0, -3px, 0);
      box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
    }

    img {
      width: 306px;
      height: 306px;
      object-fit: cover; /* 确保图片填充容器 */
    }

    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }

    .price {
      color: $priceColor;
    }
  }
}
</style>